﻿using com.uplus.log4net;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApi.Models;

namespace WebApi
{
    /// <summary>
    /// Conrtroller基类
    /// </summary>
    public abstract class BaseController: ControllerBase
    {
        /// <summary>
        /// 用户身份验证
        /// </summary>
        /// <param name="res">响应数据</param>
        /// <returns></returns>
        
        [HttpGet]
        public bool Identification(out ResponseData res)
        {
            bool re = false;
            res = new ResponseData();
            if (string.IsNullOrEmpty(ServiceBase.Get<LoginInfo>().CustCode))
            {
                res.ErrorCode = 2;
                res.Message = "非法用户请重新登录！";
                res.DataList = null;
            }
            else
            {
                re = true;
            }
            return re;
        }
        /// <summary>
        /// Conrtroller异常处理器
        /// </summary>
        /// <param name="res">响应数据</param>
        /// <param name="message">日志消息</param>
        /// <param name="ex">日志异常</param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult ControllerExHandler(dynamic res, string message, Exception ex)
        {
            res.code = 202;
            res.msg = "系统异常";
            Log4Helper.Error(this.GetType(), message, ex);
            return new JsonResult(res);
        }

        [HttpGet]
        public JsonResult AppExHandler(dynamic res, string message, Exception ex)
        {
            res.ErrorCode = 1;
            res.Message = "获取数据失败!";
            res.DataList = null;
            Log4Helper.Error(this.GetType(), message, ex);
            return new JsonResult(res);
        }


    }
}
